<template>
	<view class="order">
		<view class="popup-add-box" v-if="popupGoodsShow">
			<uni-forms :rules="baseFormRules" ref="baseForm" label-width="200rpx" label-align="right"
				:modelValue="baseFormData">
				<!-- <uni-forms-item label="基地" name="produceAddress" required>
					<uni-data-select filterable :clear='false' v-model="baseFormData.produceAddress" :localdata="jdList"
						placeholder="请选择基地"></uni-data-select>
				</uni-forms-item> -->
				<uni-forms-item label="客户" name="userId" required>
					<!-- <uni-data-select filterable :clear='false' v-model="baseFormData.userId" :localdata="khList"
						placeholder="请选择客户"></uni-data-select> -->
					<zxz-uni-data-select filterable :clear='false' :filterable='true' v-model="baseFormData.userId"
						:localdata="khList" placeholder="请选择客户"></zxz-uni-data-select>

				</uni-forms-item>
				<uni-forms-item :label="baseFormData.orderSelctProductName?'您选择的商品(点击修改)':'请选择商品'" required
					name="orderSelctProductName">
					<uni-easyinput v-if="!baseFormData.orderSelctProductName" :clearable='false' readonly="true"
						@focus="selGoods" v-model="baseFormData.orderSelctProductName" placeholder="请选择商品" />
					<view v-else class="sel-popup">
						<view class="popup-content sel-popup-content" style="position: relative;"
							v-for="val in orderSelctProduct" :key="val.id">
							<view class="popup-content-left">

								<view class="popup-box-custom-cover" @click="selGoods">
									<image class="popup-box-cover-image" mode="aspectFill" :src="val.imagePath">
									</image>
								</view>
								<view class="popup-content-text" @click="selGoods">
									<text>商品名称：{{val.productName}}</text>
									<text>品种：{{val.categoryParentName}}</text>
									<text>分类：{{val.categoryName}}</text>
									<text>规格：<span>{{val.sku}}</span></text>
									<text>库存：{{val.stockNum}}</text>
									<text>最小起订数量：{{val.minBuyCount}}</text>
									<text>定金比例：{{val.depositRatio}}</text>
									<text>购买数量：{{val.buyNum}}</text>
									<text>商品类型：
										<!-- 10-销售 20-赠品 30-赔付 -->
										{{val.orderProductType == 10?'销售':val.orderProductType == 20?'赠品':val.orderProductType == 30?'赔付':''}}
									</text>
									<!-- <text>购买数量：
									<uni-easyinput style="width: 100rpx;display: inline-block;" :clearable='false' v-model="val.buyNum" placeholder="请输入数量" @input.stop="($event)=>selCoupon($event,val)" />
									</text> -->
									<!-- <text>优惠金额：￥{{val.couponPrice || '0.00'}}</text>
									<text>种植基地：{{val.produceAddressTxt}}</text> -->
								</view>

								<uni-icons v-if="orderSelctProduct && orderSelctProduct.length > 1" type="close"
									style="color: red;position: absolute;top: -10px;right:0px;" size="22"
									@click="closeOrder(val)"></uni-icons>
							</view>
						</view>
					</view>
				</uni-forms-item>
				<uni-forms-item v-if="couponList.length > 0" label="优惠券" :required='false' name="produceAddress">
					<view>
						<view @click="onTogglePopupCoupon" v-if="userCouponId">
							{{userCouponVo.name}} >
						</view>
						<view v-else class="hascoupon" @click="onTogglePopupCoupon">
							{{ couponList.length }}张可用<text class="icon iconfont icon-jiantou"></text>
						</view>
					</view>
					<!-- <text v-else class="f24 gray9">无优惠券可用</text> -->
				</uni-forms-item>
				<uni-forms-item v-if="giftProduct && giftProduct.productName" label="赠品信息">
					<text class="f24 gray9">商品名称：{{giftProduct && giftProduct.productName}}</text>
					<br />
					<text
						class="f24 gray9">商品规格：{{giftProduct && (giftProduct.productAttr || (giftProduct.skuVo && giftProduct.skuVo.productAttr))}}</text>
					<br />
					<text class="f24 gray9">赠送数量：{{giftProduct && giftProduct.buyNum}}</text>
				</uni-forms-item>
				<uni-forms-item label="自提门店" required name="storeId">
					<uni-data-select filterable :clear='false' :localdata="userAddressList"
						v-model="baseFormData.storeId">
						<!-- <template v-slot:item="{ item }">
							<view class="custom-item">
							  <text>{{ item.storeName }}</text>
							  <br />
							  <text>{{ item.address }}</text>
							</view>
						  </template> -->
					</uni-data-select>
				</uni-forms-item>
				<uni-forms-item label="订单类型" required>
					<uni-data-checkbox v-model="baseFormData.orderType" :localdata="orderTypeList" />
				</uni-forms-item>
				<uni-forms-item label="新订单信息" v-if="JSON.stringify(CalcData) != '{}'">
					<text class="f24 gray9">优惠金额：￥{{CalcData.couponPrice || '0.00'}}</text>
					<br />
					<text class="f24 gray9">定金：￥{{CalcData.depositTotalPrice || '0.00'}}</text>
					<br />
					<text class="f24 gray9">尾款：￥{{CalcData.finalTotalPrice || '0.00'}}</text>
					<br />
					<text class="f24 gray9">包装费：￥{{CalcData.packagePrice || '0.00'}}</text>
					<br />
					<text class="f24 gray9">总金额：￥{{CalcData.totalPrice || '0.00'}}</text>
					<br />
					<text class="f24 gray9">优惠后总金额：￥{{CalcData.orderPayPrice || '0.00'}}</text>
				</uni-forms-item>
			</uni-forms>
			<view class="button-group">
				<button type="primary" size="mini" v-loading="submitLoading" @click="submit()">提交</button>
			</view>
		</view>
		<!-- 选择商品弹窗 -->
		<!-- <uni-popup ref="popupGoods" type="bottom" background-color="#fff"> -->
		<view class="popup-goods-box" v-else>
			<uni-section title="选择商品" type="line">
				<template v-slot:right>
					<!-- 选择 -->
					<view style="display: flex;">
						<uni-easyinput prefixIcon="search" v-model="baseFormData.productName" placeholder="请搜索商品名称">
						</uni-easyinput>
						<uni-icons style="color: #077840;" type="checkmarkempty" class="icons-text" size="35"
							@click="addGoods"></uni-icons>
					</view>
				</template>
				<uni-card :isFull="true">
					<template v-slot>
						<mescroll-body bottom-offset="20" ref="mescrollRef" @init="mescrollInit" @down="downCallback"
							@up="upCallback" :down="downOption" :up="upOption">
							<view class="">

								<view class="popup-content" v-for="val in spList" :key="val.id">
									<view class="popup-content-left">
										<checkbox-group @change="($event)=>checkboxChange($event,val)">
										<!-- val.stockNum == 0 || -->
											<checkbox :checked="val.check" />
										</checkbox-group>
										<view class="popup-box-custom-cover">
											<image class="popup-box-cover-image" mode="aspectFill" :src="val.imagePath">
											</image>
										</view>
										<view class="popup-content-text">
											<text>商品名称：{{val.productName}}</text>
											<text>品种：{{val.categoryParentName}}</text>
											<text>分类：{{val.categoryName}}</text>
											<text>规格：
												<!-- <template v-if="val.check">
												<span
													v-for="(item, index) in val.productAttr && ((val.productAttr.split(';')).splice(0, 2))"
													:key="index">
													{{ item.split(":")[0] + ':' }}
													<uni-tag :inverted="true" :text='item.split(" :")[1]'
														type="primary" />
												</span>
												<span style="display: flex; margin: 2px 0px;">颜 色：<uni-tag
														style="margin: 0 2px;" :key="_index"
														v-for="(cor,_index) in val.colorList"
														@click="tagClick(val, cor)" :inverted="false" :text="cor.color"
														:type="cor.check && 'primary'"></uni-tag></span>
											</template> -->
												<span>{{val.productAttr}},{{val.sku}}</span>

											</text>
											<text>库存：{{val.stockNum}}</text>
											<text>最小起订数量：{{val.minBuyCount}}</text>
											<!-- 	<text>定金比例：{{val.depositRatio}}</text>
										<text>优惠金额：￥{{val.couponPrice || '0.00'}}</text>
										<text>种植基地：{{val.produceAddressTxt}}</text> -->
										</view>
									</view>
									<view v-if="val.check" class="popup-content-right"
										style="text-align: right;width: min-content;">
										<!-- <uni-number-box @change="selCoupon(val)" :min="val.minBuyCount" :max="val.stockNum"
										:value="val.buyNum" /> -->
										<uni-easyinput
											style="width: 160rpx;display: inline-block;margin-bottom:5px;text-align: center;"
											:clearable='false' v-model="val.buyNum" placeholder="请输入数量"
											@input="($event)=>selCoupon($event,val,'buyNum')" />
										<uni-data-select @change="($event)=>selCoupon($event,val)"
											style="width: 160rpx;display: inline-block;text-align: center;" filterable
											:clear='false' v-model="val.orderProductType"
											:localdata="orderProductTypeList" placeholder="商品类型"></uni-data-select>
										<uni-easyinput
											style="width: 160rpx;display: inline-block;margin-top:5px;text-align: center;"
											:clearable='false' v-model="val.remark" placeholder="请输入备注" />
									</view>

								</view>
							</view>
						</mescroll-body>
					</template>
				</uni-card>
			</uni-section>
		</view>
		<!-- </uni-popup> -->
		<!--优惠券-->
		<Coupon :isCoupon="isCoupon" :couponList="couponList" @close="closeCouponFunc"></Coupon>
		<!-- 商品券弹窗选择 -->
		<selectProduct :isCoupon="isSelectProduct" :couponList="orderSelctProduct" @close="closeSelectProduct">
		</selectProduct>
	</view>

</template>
<script>
	import uniCalendar from '@/components/uni-calendar/uni-calendar.vue'
	import wPicker from "@/components/w-picker/w-picker.vue";
	import Coupon from "@/components/coupon.vue"
	import selectProduct from "@/components/select-product.vue"
	// 添加下拉刷新组件
	import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins.js";
	import pop from "@/components/ming-pop/ming-pop.vue"
	export default {
		mixins: [MescrollMixin],
		components: {
			uniCalendar,
			wPicker,
			pop,
			Coupon,
			selectProduct
		},
		data() {
			return {
				CalcData: {},
				searchGoodsVal: '',
				baseFormData: {
					orderType: 20,
					produceAddress: ''
				},
				newSel: [],
				popupGoodsShow: true,
				jdList: [],
				orderProductTypeList: [{
						text: '销售',
						value: 10
					},
					{
						text: '赠品',
						value: 20
					},
					{
						text: '赔付',
						value: 30
					}
				],
				submitLoading: false,
				userAddressList: [],
				orderSelctProduct: [], // 选择的商品
				giftProduct: {}, //赠品
				couponList: [], //优惠券
				userCouponId: '', //选择的优惠券
				isCoupon: false, //优惠券弹框
				isSelectProduct: false, //优惠券选择商品弹框
				userCouponVo: {},
				khList: [],
				spList: [],
				orderTypeList: [{
					text: '自提',
					value: 20
				}],
				baseFormRules: {
					// produceAddress: {
					// 	rules: [{
					// 			required: true,
					// 			errorMessage: '请选择基地',
					// 		},

					// 	],
					// },
					userId: {
						rules: [{
								required: true,
								errorMessage: '请选择客户',
							},

						],
					},
					orderSelctProductName: {
						rules: [{
								required: true,
								errorMessage: '请选择商品',
							},

						],
					},
					storeId: {
						rules: [{
								required: true,
								errorMessage: '请选择自提门店',
							},

						],
					}
				},

				backForm: {
					orderNumber: '',
					orderId: '',
					paySource: '',
					payTime: '',
					tradeNo: '',
					fileId: ''
				},
				tab: [{
						name: '全部',
						value: 0,
						filter: 'all'
					},
					{
						name: '待支付',
						value: 1,
						filter: 'payment'
					},
					{
						name: '待确认',
						value: 2,
						filter: 'notConfirm'
					},
					{
						name: '待付尾款',
						value: 3,
						filter: 'notFinalPayment'
					},
					{
						name: '待发货',
						value: 5,
						filter: 'delivery'
					},
					// fix 取消
					// {
					// 	name: '待自提',
					// 	value: 4,
					// 	filter: 'notPickup'
					// },

					{
						name: '待收货',
						value: 6,
						filter: 'received'
					},
					// fix 取消 
					// {
					// 	name: '待评价',
					// 	value: 7,
					// 	filter: 'notComment'
					// },
					// {
					// 	name: '已评价',
					// 	value: 8,
					// 	filter: 'commented'
					// },
					{
						name: '退款',
						value: 10,
						filter: 'refund'
					},
					{
						name: '已取消',
						value: 11,
						filter: 'cancel'
					}
				],
				refOptions: [20, 30, 40],
				datalist: [],
				datalist1: [],

				curren: 0,
				goodsList: [],
				title: 'picker',
				selectorList: [],
				defaultProps: {
					"label": "title"
				},
				index: 0,
				calendar: false,
				isShow: false,
				visible: false,
				length: 0,
				date: '',
				id: 0,
				// 下拉刷新的组件 参数 可以 改变的
				downOption: {
					use: false,
					auto: false //是否在初始化后,自动执行downCallback; 默认true
				},
				upOption: {
					page: {
						num: 0,
						size: 10,
						time: null
					},
					offset: 200,
					onScroll: true,
					auto: false, // 不自动加载
					noMoreSize: 3, //如果列表已无数据,可设置列表的总数量要大于半页才显示无更多数据;避免列表数据过少(比如只有一条数据),显示无更多数据会不好看; 默认5
					empty: {
						tip: '~ 空空如也 ~', // 提示
						icon: '/static/home/saber.png'
					},
					textNoMore: '—— 没有更多了 ——'
				},
				order_goods: [],
				info: {
					lunar: true,
					range: false,
					insert: false,
					selected: [],
					data: '',
					startDate: ''
				},

				department: '',
				indexd: 0,
				indexc: 0,
				pitchItem: '', //选中的item
				isNoEX: true,
				pickItem: {},

				orderConfirmItem: {}, //确认订单选择数据
				orderConfirmModel: {
					orderId: '',
					pickupTime: '',
					paySource: '',
					payTime: '',
					tradeNo: '',
					payType: 0,
					timeType: '',
					fileId: ''

				},
				odForm: {
					orderId: 0,
					refundPrice: 0,
					orderPrice: 0,
					reason: '',
					fileId: '',
					odFilePath: ''
				},
				selectFiles: [],
				wkSelectFiles: [],
				setTimeProduct: null
			}
		},
		computed: {
			filteredList() {
				return this.spList.filter(item => {
					return item.productName.toLowerCase().includes(this.searchGoodsVal.toLowerCase());
				});
			}
		},
		witch: {
			spList: {
				handler(v) {
					console.log(v,'vvvv');
				},
				// 开启深度监听
				deep: true
			}
		},
		// 顶部返回按钮控制
		onBackPress() {
			console.log('我点击了')
			console.log(this.popupGoodsShow, 'this.popupGoodsShow')
			// 生成订单的页面 popupGoodsShow
			if (this.popupGoodsShow) {
				uni.switchTab({
					url: '/pages/orderList/orderList'
				})
				return true;
			} else {
				this.popupGoodsShow = true;
				return false;
			}
		},
		onLoad(options) {
			this.baseFormData = {
				orderType: 20
			};
			this.CalcData = {};
			this.orderSelctProduct = [];
			// 获取基地
			// this.produceListFun();
			// 直接获取商品
			this.productPageSkuFun();
			// 获取客户
			this.userIndex();
		},
		watch: {
			"baseFormData.produceAddress": {
				handler(v) {
					if (v) {
						this.productPageSkuFun();
					}
				},
				deep: true,
			},
			"baseFormData.productName": {
				handler(v) {
					this.productPageSkuFun();
				},
				deep: true,
			},

		},
		methods: {
			downCallback() {
				this.mescroll.resetUpScroll()
			},
			// 生成订单提交
			submit(baseForm) {
				this.$refs.baseForm.validate().then(res => {
					this.submitLoading = true;
					// 最新参数
					let obj = {
						orderId: '',
						storeId: this.baseFormData.storeId,
						couponId: this.userCouponId,
						userId: this.baseFormData.userId,
						productParams: this.orderSelctProduct,
						giftProduct: this.giftProduct
					}
					this.api.shopUserCreate(obj).then(res => {
						const {
							code,
							msg,
							data
						} = res.data;
						console.log(res, 'res')
						this.submitLoading = false;
						if (code === 1) {
							this.tui.toast(msg)
							// this.$refs.addPopup.close();
							// 刷新列表
							// this.orderListAll('all')
							// 到详情 redirectTo navigateTo
							uni.redirectTo({
								url: '/pages/orderList/orderListDetail/orderListDetail?orderId=' +
									data
							})
						}else{
							this.tui.toast(msg)
						}
					})
				}).catch(err => {
					console.log('表单错误信息：', err);
				})
			},

			/*选择优惠卷*/
			onTogglePopupCoupon() {
				console.log(11111)
				let self = this;
				self.isCoupon = true;
				self.couponList = this.couponList;
			},
			/*关闭优惠券*/
			closeCouponFunc({
				item,
				couponType,
				e
			}) {
				console.log(item, couponType, e, 'werrew')
				// if (!e) return;
				this.isCoupon = false;
				this.userCouponId = e || 0;
				this.userCouponVo = item
				if (couponType == 40) {
					this.isSelectProduct = true;
				}
				this.$nextTick(() => {
					this.orderModifyCalc();
				})

			},
			// 关闭选择优惠券商品
			closeSelectProduct({
				row,
				e
			}) {
				console.log(row, 'closeSelectProduct')
				this.giftProduct = row;
				this.isSelectProduct = false;
				this.$nextTick(() => {
					this.orderModifyCalc();
				})
			},
			// 获取地址
			loadKhDetial(id) {

				this.api.storeIndex({
						pageIndex: 1,
						pageSize: 10000,
					}, true)
					.then((result) => {
						console.log(result, 'result')
						const {
							code,
							data
						} = result.data;
						if (code == 1) {
							// data.userAddressList.map(val => {
							// 	val['text'] =
							// 		`${val.provinceName}/${val.cityName}/${val.regionName}/${val.detail}`
							// 	val['value'] = val.addressId
							// })
							this.userAddressList = [];
							if (data.records) {
								data.records.map(val => {
									val['value'] = val.storeId
									val['text'] = val.storeName
								})
							}
							this.userAddressList = data.records;
							console.log(this.userAddressList, 'this.userAddressList ')
						}
					})

			},
			tagClick(row, cor) {
				row.colorList.map(val => val.check = false)
				cor.check = !cor.check;
				if (cor.check) {
					row.productSkuId = cor.productSkuId
				}
			},
			// 获取规格
			skuList(_row, productId) {
				this.api.skuList({
					productId
				}).then(res => {
					const {
						code,
						data
					} = res.data;
					console.log(data, 'data-----')
					_row['colorList'] = data
					//默认选择第一个
					_row['colorList'][0]['check'] = true;
					_row['productSkuId'] = data[0]['productSkuId']
					this.spList = JSON.parse(JSON.stringify(this.spList))

				})
			},
			// 生成订单删除
			closeOrder(row) {
				const set1 = new Set([row].map(item => item['productSkuId']))
				const set2 = new Set(this.orderSelctProduct.map(item => item['productSkuId']))
				const diff = [
					...[row].filter(item => !set2.has(item['productSkuId'])),
					...this.orderSelctProduct.filter(item => !set1.has(item['productSkuId']))
				]
				this.orderSelctProduct = diff;
				this.orderSelctProduct = JSON.parse(JSON.stringify(this.orderSelctProduct))
				this.$nextTick(() => {
					this.orderModifyCalc();
				})
			},
			addGoods() {
				// this.popupGoodsShow = true;
				// this.newSel = [];
				// this.spList.map(val => {
				// 	if (val.check) {
				// 		this.newSel.push(val)
				// 	}
				// })
				setTimeout(() => {

					if (this.newSel.some(val => !val.orderProductType)) {
						return this.tui.toast('请选择商品类型!')
					}
					this.$set(this, 'popupGoodsShow', true)
					// console.log(this.spList.filter(val => val.check), '选中的商品');
					// this.orderSelctProduct = this.spList.filter(val => val.check);
					// 重新修改匹配数据
					// let selNewArr= []
					// const ids = new Set(this.orderSelctProduct.map(item => item.productSkuId));
					// 返回array1中id也存在于array2的元素
					// selNewArr = this.newSel.filter(item => ids.has(item.productSkuId));
					// console.log(selNewArr,'selNewArr')
					// this.orderSelctProduct = selNewArr;
					// 之前的计算
					// if(this.newSel && this.newSel.length){
					// 	selNewArr = this.newSel.filter(newS => this.orderSelctProduct.some(ord=> newS.productSkuId == ord.productSkuId))
					// }
					console.log(this.orderSelctProduct,'this.orderSelctProduct')
					this.orderSelctProduct = this.orderSelctProduct.concat(this.spList.filter(val => val.check))
					this.orderSelctProduct = [...new Map(this.orderSelctProduct.map(item => [item.productSkuId,
						item
					])).values()]
					if (this.orderSelctProduct.length) {
						this.baseFormData.orderSelctProductName = (this.orderSelctProduct.map(val => val
							.productName)).join(',')
					}
					this.baseFormData = JSON.parse(JSON.stringify(this.baseFormData))
					this.loadKhDetial(this.baseFormData.userId);
					// this.$refs.popupGoods.close()
					//计算接口
					this.$nextTick(() => {
						this.orderModifyCalc();
					})
				}, 500)
			},
			async checkboxChange(e, row) {
				// if (row.stockNum == 0 && !row.check) {
				// 	row.check = false
				// 	this.tui.toast('该商品库存不足!')
				// } else {
					row.check = !row.check;
					if (row.check && !row.buyNum) {
						row.buyNum = row.minBuyCount
					}
					if(row.check){
						row.orderProductType = 10;
					}
				// }

				// 选中之后选中规格
				// if (row.check) {
				// 	await this.skuList(row, row.productId);
				// }
			},
			upCallback(page) {
				console.log(page, 'page')
				let pageNum = page.num; // 页码, 默认从1开始
				let pageSize = page.size;
				const $this = this
				this.api.productPageSku({
					pageIndex: pageNum,
					pageSize,
					produceAddress: this.baseFormData.produceAddress
					// productType:1 // 普通商品 1
				}).then(res => {
					const {
						code,
						data
					} = res.data;
					console.log(data, 'data.records')
					// if (page.num == 1) this.spList = [];
					const totalCount = data.total
					const orderLength = data.records.length
					this.mescroll.endBySize(orderLength, totalCount);
					// if (data.records) {
					data.records.map(val => {
						val['value'] = val.userId
						val['text'] = val.realName
						val['check'] = false;
					})
					// this.spList = data.records;
					let spList = this.spList.concat(data.records);
					this.spList = [...new Map(spList.map(item => [item.productSkuId, item])).values()]
					// }
					// if (data.productList && data.productList.records) {
					// 	data.productList.records.map(val => {
					// 		val['value'] = val.userId
					// 		val['text'] = val.realName
					// 		val['check'] = false;
					// 	})
					// 	this.spList = data.productList.records;
					// }
					
					// 每次获取新的数据 都需要和直接选择的对比
					// 回显选中的
					if (this.orderSelctProduct && this.orderSelctProduct.length) {
						console.log(this.orderSelctProduct, 'this.orderSelctProduct')
						// this.newSel = []
						this.spList.map(val => {
							this.orderSelctProduct.map(item => {
								console.log(item.orderProductType, 'item.orderProductType')
								if (item.productSkuId == val.productSkuId) {
									val['check'] = true
									// this.newSel.push(item)
									val.buyNum = item.buyNum
									val['orderProductType'] = item.orderProductType
								}
							})
						})
					}
				}).catch(e => {
					this.mescroll.endSuccess()
				})
			},
			// 获取商品
			productPageSkuFun() {
				// productLists 
				this.api.productPageSku({
					pageIndex: 1,
					pageSize: 10,
					// produceAddress: this.baseFormData.produceAddress,
					productName: this.baseFormData.productName
					// productType:1 // 普通商品 1
				}).then(res => {
					const {
						code,
						data
					} = res.data;
					console.log(data, 'data.records')
					// 搜索不置空
					this.spList = [];

					if (data.records) {
						data.records.map(val => {
							val['value'] = val.userId
							val['text'] = val.realName
							val['check'] = false;
						})

						this.spList = data.records;
						
						// 回显选中的
						if (this.orderSelctProduct && this.orderSelctProduct.length) {
							console.log(this.orderSelctProduct, 'this.orderSelctProduct')
							// this.newSel = []
							this.spList.map(val => {
								this.orderSelctProduct.map(item => {
									console.log(item.orderProductType, 'item.orderProductType')
									if (item.productSkuId == val.productSkuId) {
										val['check'] = true
										// this.newSel.push(item)
										val.buyNum = item.buyNum
										val['orderProductType'] = item.orderProductType
									}
								})
							})
						}

					}
					// if (data.productList && data.productList.records) {
					// 	data.productList.records.map(val => {
					// 		val['value'] = val.userId
					// 		val['text'] = val.realName
					// 		val['check'] = false;
					// 	})
					// 	this.spList = data.productList.records;
					// }
				})
			},
			// 获取客户
			userIndex() {
				this.api.userIndex({
					pageIndex: 1,
					pageSize: 999999,
					filterType: 'shopUser'
				}).then(res => {
					const {
						code,
						data
					} = res.data;
					console.log(data.records, 'data.records')
					if (data.records) {
						data.records.map(val => {
							val['value'] = val.userId
							val['text'] = val.realName
						})
						this.khList = data.records;
					}
				})
			},
			// 选择优惠券 编辑数量
			selCoupon(e, val, type = '') {
				console.log(val, 'val')
				if (Number(e) > val.stockNum) {
					this.tui.toast('超过库存数量了')
					return
				}
				if (type == 'buyNum') {
					val.buyNum = e;
				}
				this.$forceUpdate();
				this.spList = JSON.parse(JSON.stringify(this.spList))
				this.$nextTick(() => {
					this.orderModifyCalc();
				})
			},
			// 计算接口
			orderModifyCalc() {

				let Obj = {
					orderId: '',
					couponId: this.userCouponId,
					userId: this.baseFormData.userId,
					productParams: this.orderSelctProduct,
					giftProduct: this.giftProduct
				}
				this.api.orderModifyCalc(Obj, true).then(res => {
					console.log(res, 'res')
					const {
						code,
						data,
						msg
					} = res.data;
					if (code == 1) {
						// 数据清空
						this.couponList = [];
						this.userCouponId = '';
						this.CalcData = data;
						this.couponList = data.couponList;
						data.userCouponVo && (this.userCouponId = data.userCouponVo.userCouponId)
						data.userCouponVo && (this.userCouponVo = data.userCouponVo)
						// 赠品
						// if (data.giftProduct) {
						this.giftProduct = data.giftProduct;
						// }
						// 商品信息
						if (data.productList) {
							this.orderSelctProduct.map(val => {
								data.productList.map(item => {
									if (val.productId == item.productId) {
										val.couponPrice = item.couponPrice
										val.depositPrice = item.depositPrice
										val.packagePrice = item.packagePrice
										val.totalPayPrice = item.totalPayPrice
										val.finalPrice = item.finalPrice
									}
								})
							})
						}
						// 强制刷新
						this.couponList = JSON.parse(JSON.stringify(this.couponList))
					} else {
						this.tui.toast(msg)
					}

				}).catch(e => {
					this.slCaleLoading = false;

				})
			},
			// 生成订单
			fabClick() {
				this.baseFormData = {
					orderType: 20
				};
				this.CalcData = {};
				this.orderSelctProduct = [];
				this.$refs.addPopup.open()
				this.userIndex();
				// 获取基地
				this.produceListFun();
			},
			//获取基地集合
			produceListFun() {
				this.api.produceList().then(res => {
					const {
						code,
						data
					} = res.data;
					if (code == 1) {
						data.map(val => {
							val['value'] = val.produceAddrId
							val['text'] = val.produceName
						})
						this.jdList = data;
					}
				})
			},
			selGoods() {
				uni.hideKeyboard();
				this.$set(this, 'popupGoodsShow', false)
				// this.$refs.popupGoods.open()
				// 回显选中的
				if (this.orderSelctProduct && this.orderSelctProduct.length) {
					console.log(this.oldSel, 'this.oldSel')
					// this.newSel = []
					this.spList.map(val => {
						this.orderSelctProduct.map(item => {
							console.log(item.orderProductType, 'item.orderProductType')
							if (item.productSkuId == val.productSkuId) {
								val.check = true
								// this.newSel.push(item)
								val.buyNum = item.buyNum
								val['orderProductType'] = item.orderProductType
							}
						})
					})
				}
			},

			onTest() {
				window.open('#小程序://上云丰/vmIQrRgFtQks5Fv')
			},

			// 搜索
			search() {
				uni.navigateTo({
					url: '/pages/search/searchs'
				})
			},
		},
	}
</script>
<style scoped>
	/deep/.uni-fab__circle {
		width: 30px;
		height: 30px;
	}

	/deep/.uni-forms-item {
		margin-bottom: 20rpx !important;
	}

	/deep/.uni-section-content {
		/* height:85vh !important; */
		overflow-y: auto !important;
		margin-top: 65px;
	}

	/deep/.uni-section .uni-section-header {
		position: fixed;
		top: 45px;
		z-index: 2000;
		width: 95%;
		background: #fff;
	}

	/deep/.uni-select__selector-scroll {
		max-height: 230rpx !important;
	}

	/deep/.uni-section .uni-section-header__slot-right {
		width: 75% !important;
	}

	/deep/.usable-coupon .main {
		margin-bottom: 0px !important;
	}
</style>
<style lang="less" scoped>
	.hascoupon {
		padding: 0 18rpx;
		box-sizing: border-box;
		height: 38rpx;
		line-height: 38rpx;
		border-radius: 200rpx;
		color: #ffffff;
		background: linear-gradient(180deg, #fc4133, #ff7a04);
		font-size: 20rpx;
		width: fit-content;

		.icon.icon-jiantou {
			color: #ffffff;
			font-size: 20rpx;
			margin-left: 2rpx;
		}
	}

	.button-group {
		position: fixed;
		bottom: 0rpx;
		/* left: 40%; */
		width: 100%;
		background: #fff;
		padding-top: 10px;
	}

	.popup-content-text {
		uni-text {
			display: block;
			// width: 540rpx;
			white-space: pre-wrap;
			word-wrap: break-word;
		}
	}

	.sel-popup {
		max-height: 530rpx;
		overflow-y: auto;
		padding: 20rpx;
		box-sizing: border-box;
	}

	.popup-content {
		margin: 5px 0;
		border-bottom: 1px solid #077840;
	}

	.sel-popup-content {
		border-bottom: none;
		box-shadow: 0px 0px 8px rgba(0, 0, 0, 0.18);
		border-radius: 20rpx;
		box-sizing: border-box;
		padding: 20rpx;
	}

	.popup-add-box {
		min-height: 100vh;
		overflow-y: auto;
		margin-bottom: 90rpx;
	}

	.popup-goods-box {
		// margin-bottom: 104rpx;
	}

	.fott-btn {
		display: flex;
		gap: 20px;

		.close {
			flex: 1;
			text-align: center;
			color: red;
			padding: 10px 5px;
		}

		.confirm {
			flex: 1;
			color: #077840;
			text-align: center;
			padding: 10px 5px;
		}

	}

	.mobile {
		padding: 20rpx 0;
		box-sizing: border-box;

		margin: 0 auto;
		border-bottom: 2px solid #EEEEEE;

		span {
			font-size: 14px;
			background: #4ba170;
			color: #fff;
			padding: 5px;
			display: block;
			text-align: center;
			margin: 5px;
		}

		.filetext {
			color: #4ba170;
			padding-left: 10px;
			font-size: 12px;
		}

		.mobile-text {
			margin-bottom: 0rpx;
			font-size: 28rpx;
			font-family: PingFangSC-Regular, PingFang SC;
			font-weight: 400;
			color: #333333;
		}

		.mobile-input input:placeholder {
			font-size: 28rpx;
			margin-top: 5px;
			font-family: PingFangSC-Regular, PingFang SC;
			font-weight: 400;
			color: #999999;
		}
	}

	page {
		background-color: #F9F9F9;
	}

	.content-pop {
		// position: relative;
		position: absolute;
		top: 39%;
		left: 0;
		width: 100%;
	}

	.top {
		width: 100%;
		height: 100rpx;
		line-height: 100rpx;
		background-color: #fff;
		position: absolute;
		top: -12%;
		left: 0;
		padding: 0 30rpx;
		box-sizing: border-box;
		display: flex;
		justify-content: space-between;
		border-bottom: 1rpx solid #c8c7cc;
		font-size: 36rpx;
		font-family: PingFangSC-Regular, PingFang SC;
		font-weight: 400;
		color: #333333;

		.confirm {
			color: #077840;
		}
	}

	.order-input {
		width: 100%;
		padding-top: 30rpx;
		padding-left: 25rpx;
		padding-right: 25rpx;
		box-sizing: border-box;
		background-color: #fff;
		position: fixed;
		top: 84rpx;
		left: 0;
		z-index: 123;


		input {
			height: 72rpx;
			width: 100%;
			background-color: #F6F6F6;
			border-radius: 36px;
			padding-left: 224rpx;
			box-sizing: border-box;
			margin-bottom: 30rpx;

			&:placeholder {
				font-size: 28rpx;
				font-family: PingFangSC-Regular, PingFang SC;
				font-weight: 400;
				color: #BEBEBE;
				line-height: 72rpx;
				text-align: center;
			}

		}

		.search-icon {
			width: 29rpx;
			height: 31rpx;
			color: #BEBEBE;
			position: absolute;
			top: 52rpx;
			left: 210rpx;

			.uni-icons {
				font-size: 30rpx;
			}
		}

		.toptab {
			// height: 102rpx;
			// margin-left: 30rpx;
			overflow-x: scroll;
			white-space: nowrap;

			.item {
				height: 60rpx;
				font-size: 30rpx;
				font-family: PingFangSC-Regular, PingFang SC;
				font-weight: 400;
				color: #333333;
				// line-height: 60rpx;
				display: inline-block;
				margin-left: 30rpx;
				margin-right: 30rpx;

				// margin-top: 20rpx;
				&:first-child {
					margin-left: 10rpx;
				}
			}

			.active {
				font-weight: 500;
				color: #077840 !important;
			}

			.bottom-line {
				width: 60rpx;
				height: 4rpx;
				background: #077840;
				margin: 0 auto;
				margin-top: 16rpx;
			}
		}
	}

	.sta {
		height: 52rpx;
		background: #F3F8F6;
		border-radius: 8rpx;
		font-size: 22rpx;
		line-height: 52rpx;
		text-align: center;
		font-family: PingFangSC-Regular, PingFang SC;
		font-weight: 400;
		color: #077840;
		// margin-bottom: 30rpx;
		padding: 0 10rpx;
		box-sizing: border-box;
	}

	.goods-sta {
		display: inline-block;
	}

	.goods-sta1 {
		// width: 252rpx;
		display: inline-block;

	}

	.goods-sta2 {
		display: inline-block;
		color: #999999;
		background-color: #EEEEEE;
	}

	.item-content {
		background-color: #F9F9F9;
		padding-top: 194rpx;
		box-sizing: border-box;

		.item {
			width: 690rpx;
			// height: 600rpx;
			margin: 0 auto;
			margin-top: 20rpx;
			background-color: #fff;
			padding-bottom: 30rpx;
			box-sizing: border-box;


			&:last-child {
				margin-bottom: 20rpx;
			}
		}

		.buyers {
			.buyers-bigBox {
				margin: 0 30rpx;

				.buyers-box {
					width: 100%;
					height: 99rpx;
					line-height: 99rpx;
					border-bottom: 2px solid #EEEEEE;
					display: flex;
					justify-content: space-between;

					.buyers-left {
						display: flex;
						justify-content: flex-start;

						.courier {
							width: 108rpx;
							height: 52rpx;
							border-radius: 8px;
							border: 2px solid #077840;
							margin-top: 20rpx;
							font-size: 22rpx;
							font-family: PingFangSC-Regular, PingFang SC;
							font-weight: 400;
							color: #077840;
							line-height: 52rpx;
							text-align: center;
						}

						.buyers-name {
							font-size: 26rpx;
							font-family: PingFangSC-Medium, PingFang SC;
							font-weight: 500;
							color: #333333;
							margin-right: 20rpx;
						}
					}

					.buyers-right {
						.buyers-mobile {
							font-size: 26rpx;
							font-family: PingFangSC-Regular, PingFang SC;
							font-weight: 400;
							color: #077840;
							margin-right: 20rpx;
						}

						.goods-status {
							font-size: 26rpx;
							font-family: PingFangSC-Regular, PingFang SC;
							font-weight: 400;
							color: #077840;
						}
					}

				}

				.goods-detail {
					// height: 350rpx;
					width: 100%;
					border-bottom: 2px solid #EEEEEE;
					margin-top: 28rpx;

					.goods-newly {
						margin-bottom: 28rpx;
						display: flex;
						justify-content: space-between;

						&_icon {
							width: 100rpx;
							height: 50rpx;
							font-size: 24rpx;
							font-family: PingFangSC-Medium, PingFang SC;
							font-weight: 500;
							color: #FFFFFF;
							line-height: 50rpx;
							background: linear-gradient(54deg, #FF7436 0%, #FF3333 100%);
							border-radius: 16rpx 0px 16rpx 0px;
							text-align: center;
						}

						/* 秒杀颜色 */
						&_color {
							background: linear-gradient(270deg, #FF904C 0%, #FF5F43 47%, #FF4B90 100%);
						}
					}

					.goods-top {
						display: flex;
						justify-content: flex-start;

						.goods-img {
							width: 180rpx;
							height: 180rpx;
							margin-right: 20rpx;

							image {
								width: 100%;
								height: 100%;
							}
						}

						.goods-detail-text {
							height: 180rpx;
							width: 430rpx;

							.goods {
								display: flex;
								justify-content: space-between;
								margin-bottom: 10rpx;
								font-size: 28rpx;
								font-family: PingFangSC-Regular, PingFang SC;
								font-weight: 400;
								color: #333333;
							}

							.goods-species {
								display: flex;
								justify-content: space-between;
								font-size: 24rpx;
								font-family: PingFangSC-Regular, PingFang SC;
								font-weight: 400;
								color: #999999;
							}
						}
					}

					.packing {
						display: flex;
						justify-content: space-between;
						font-size: 26rpx;
						font-family: PingFangSC-Regular, PingFang SC;
						font-weight: 400;
						color: #999999;
						margin: 20rpx 0;

						.packing_right {
							flex: 1;
							margin-left: 30rpx;
						}
					}

					.total-price {
						font-size: 26rpx;
						font-family: PingFangSC-Regular, PingFang SC;
						font-weight: 400;
						color: #999999;
						margin-bottom: 30rpx;

						.total {
							color: #333333;
						}
					}
				}

				.order-number {
					font-size: 26rpx;
					font-family: PingFangSC-Regular, PingFang SC;
					font-weight: 400;
					color: #999999;
					margin-top: 28rpx;

					.number {
						color: #333333;
					}
				}

				.order-time {
					font-size: 26rpx;
					font-family: PingFangSC-Regular, PingFang SC;
					font-weight: 400;
					color: #999999;
					margin-top: 20rpx;
					margin-bottom: 30rpx;

					.time {
						color: #333333;
					}
				}

				.order-btn {
					font-size: 26rpx;
					font-family: PingFangSC-Regular, PingFang SC;
					font-weight: 400;
					display: flex;
					justify-content: flex-end;

					.btn {
						width: 136rpx;
						height: 50rpx;
						line-height: 50rpx;
						text-align: center;
						background: #FFFFFF;
						border-radius: 8rpx;
					}

					.refund-btn {
						border: 2rpx solid #CECECE;
						// box-sizing: border-box;
						color: #666666;
					}

					.confirm-btn {
						border: 2rpx solid #077840;
						// box-sizing: border-box;
						color: #077840;
						margin-left: 10rpx;
					}

				}
			}
		}
	}

	.common {
		display: flex;
		justify-content: flex-end;
	}

	.sta-sty {
		view {
			margin-bottom: 0;
			margin: 4rpx 0 !important;
		}

		.sta1 {
			margin-right: 312rpx !important;
		}

		.sta2 {
			margin-right: 226rpx !important;
		}

		.sta3 {
			margin-right: 268rpx !important;
		}
	}

	/deep/uni-model .uni-model {
		width: 560rpx;
		height: 360rpx;
		background: #FFFFFF;
		border-radius: 16rpx;
	}

	/deep/uni-modal .uni-modal__title {
		font-size: 36rpx;
		font-family: PingFangSC-Medium, PingFang SC;
		font-weight: 500;
		color: #333333;
	}

	/deep/uni-modal .uni-modal__bd {
		font-size: 30rpx;
		font-family: PingFangSC-Regular, PingFang SC;
		font-weight: 400;
		color: #333333;
	}

	.pop-up {
		width: 100%;
		height: 43%;
		background: rgba(0, 0, 0, .7);
		position: fixed;
		top: 0;
		left: 0;
		z-index: 9999;

		.calendar {
			width: 750rpx;
			height: 608rpx;
			background: #FFFFFF;
			border-radius: 16rpx 16rpx 0px 0px;
			position: absolute;
			bottom: 0%;
			left: 0%;
			// padding: 0 10rpx;
			box-sizing: border-box;
		}
	}

	/deep/.w-picker-header uni-text:last-child {
		color: #077840 !important;
	}

	.time1 {
		font-size: 24rpx;
		font-family: PingFangSC-Regular, PingFang SC;
		font-weight: 400;
		color: #FF6269;
		margin: 20rpx 0rpx;
	}

	.quanbu {
		width: 180rpx !important;
		background-color: #EEEEEE !important;
		color: #999999 !important;
		border: 0 !important;
	}

	.btns {
		border: 1px solid #097840;
		padding: 5px;
		border-radius: 5px;
		color: #097840;
	}
</style>